package com.jwsoft.manager.core.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.jwsoft.manager.common.enums.MatchingStatusEnum;
import com.jwsoft.manager.core.dao.mapper.EduAddressCategoryMapper;
import com.jwsoft.manager.core.dao.mapper.EduAddressMapper;
import com.jwsoft.manager.core.dao.mapper.EduAddressSchoolAuditMapper;
import com.jwsoft.manager.core.dao.mapper.EduAddressSchoolMapper;
import com.jwsoft.manager.core.dao.model.EduAddress;
import com.jwsoft.manager.core.dao.model.EduAddressCategory;
import com.jwsoft.manager.core.dao.model.EduAddressSchool;
import com.jwsoft.manager.core.dao.model.EduAddressSchoolAudit;
import com.jwsoft.manager.core.dao.service.EduAddressCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 地址和学段关联记录表服务实现类
 *
 * @author fangs
 * @since 2023-02-24
 */
@Service
public class EduAddressCategoryServiceImpl extends ServiceImpl<EduAddressCategoryMapper, EduAddressCategory> implements EduAddressCategoryService {

    @Autowired
    private EduAddressSchoolAuditMapper eduAddressSchoolAuditMapper;
    @Autowired
    private EduAddressSchoolMapper eduAddressSchoolMapper;
    @Autowired
    private EduAddressMapper eduAddressMapper;

    @Override
    public void checkAndUpdate(String addressId, String schoolCategory) {
        MatchingStatusEnum matchingStatus;
        //匹配成功
        //eduAddressSchool表中有该地址该学段的数据
        QueryWrapper<EduAddressSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduAddressSchool::getAddressId, addressId)
                .eq(EduAddressSchool::getSchoolCategory, schoolCategory);
        if (eduAddressSchoolMapper.selectCount(queryWrapper) > 0) {
            matchingStatus = MatchingStatusEnum.SUCCESS;
            this.updateMatchingStatus(addressId, schoolCategory, matchingStatus);
            return;
        }

        //失败  所有审核未通过
        //eduAddressSchoolAudit表中审核全不通过且eduAddressSchool表无数据
        QueryWrapper<EduAddressSchoolAudit> auditQueryWrapper = new QueryWrapper<>();
        auditQueryWrapper.lambda().eq(EduAddressSchoolAudit::getAddressId, addressId)
                .eq(EduAddressSchoolAudit::getSchoolCategory, schoolCategory)
                .eq(EduAddressSchoolAudit::getAuthStatus, AuthStatusEnum.NODO.getType());
        if (eduAddressSchoolAuditMapper.selectCount(auditQueryWrapper) == 0) {
            matchingStatus = MatchingStatusEnum.NOT_PASS;
            this.updateMatchingStatus(addressId, schoolCategory, matchingStatus);
        }
    }

    @Override
    public void checkAndUpdate(String addressId, String schoolCategory, boolean isDel) {
        if (isDel) {
            baseMapper.subSchool(addressId, schoolCategory);
            QueryWrapper<EduAddressCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(EduAddressCategory::getAddressId, addressId)
                    .eq(EduAddressCategory::getSchoolCategory, schoolCategory)
                    .select(EduAddressCategory::getSchoolNum, EduAddressCategory::getMatchingStatus, EduAddressCategory::getId);
            EduAddressCategory eduAddressCategory = baseMapper.selectOne(queryWrapper);
            if (eduAddressCategory != null && eduAddressCategory.getSchoolNum() <= 0
                    && MatchingStatusEnum.SUCCESS.getType().equals(eduAddressCategory.getMatchingStatus())) {
                UpdateWrapper<EduAddressCategory> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(EduAddressCategory::getId, eduAddressCategory.getId())
                        .set(EduAddressCategory::getMatchingStatus, MatchingStatusEnum.NOT_PASS.getType())
                        .set(EduAddressCategory::getMatchingMessage, "关联学校数减为0自动更新");
                baseMapper.update(null, updateWrapper);
            }
        } else {
            checkAndUpdate(addressId, schoolCategory);
        }
    }

    private void updateMatchingStatus(String addressId, String schoolCategory, MatchingStatusEnum matchingStatus) {
        QueryWrapper<EduAddressCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduAddressCategory::getSchoolCategory, schoolCategory)
                .eq(EduAddressCategory::getAddressId, addressId);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            baseMapper.updateMatchingStatus(addressId, schoolCategory, matchingStatus.getType(), matchingStatus.getName());
        } else {
            EduAddress eduAddress = eduAddressMapper.selectById(addressId);
            EduAddressCategory eduAddressCategory = new EduAddressCategory();
            eduAddressCategory.setAddressId(addressId);
            eduAddressCategory.setTreeId(eduAddress.getAddressTreeId());
            eduAddressCategory.setAddressName(eduAddress.getAddressName());
            eduAddressCategory.setSchoolCategory(schoolCategory);
            eduAddressCategory.setMatchingStatus(matchingStatus.getType());
            eduAddressCategory.setMatchingTime(new Date());
            eduAddressCategory.setMatchingMessage(matchingStatus.getName());
            eduAddressCategory.setSchoolNum(1);
            baseMapper.insert(eduAddressCategory);
        }

    }
}
